/*
 * File:   Command.c
 * Author: Muhammad Anis
 */

#include "Command.h"
// global variable, collection of commands
Command Instructions[100];
// tail, initialized as -1
int tail = -1;
/**
 * Make command with given name and instruction
 * @param name Name of Command
 * @param instruction Instruction mapped with name
 * @return Command with name=name and instruction=instruction
 */
Command MakeCommand(char* name, void(*instruction)(Calendar*cal,char** argv)){    
    Command c;
    c.name = name;   
    c.instruction = instruction;
    return c;
}
/**
 * Add command to array with constraint that array is not full
 * @param c
 */
void AddInstruction(Command c){
    if(tail < MAX_COMMAND - 1){
        if(!isCommandExist(c.name)){            
            tail=tail+1;
            Instructions[tail] = c;            
        }else{
            printf("Command with name %s already exist\n",c.name);
        }
    }else{
        printf("Cannot add More Command, Array is full\n");
    }
}

/**
 * Execute an instruction with given name
 * @param name name of instruction
 * @param argv instruction's parameter
 */
void Execute(char* name, Calendar *c,char** argv){
    int i=0;
    for(i=0;i<=tail;i++){
        if(EqCSString(name,Instructions[i].name)){
            (Instructions[i].instruction)(c,argv);
            break;
        }
    }
}
/**
 * Remove a command in array (if exists)
 * Not Implemented Yet
 * @param name name of command
 */
void RemoveCommand(char* name){
    //do nothing yet
    printf("This function hasn't been implemented yet\n");
}
/**
 * Check if a command with specified name already exist
 * @param name name of command
 * @return true if there is a command with name=name
 */
int isCommandExist(char* name){
    int i=0;
    for(i=0;i<=tail;i++){
        if(EqCSString(name, Instructions[i].name)){
            return true;
        }
    }
    return false;
}
/**
 * Procedure for Testing purpose
 * @param argv
 */
void Print(Calendar *c,char** argv){
    printf("Hello World\n");
}
